package by.grsu.hdm.devices;

import java.util.Random;

import by.grsu.hdm.model.HomeDevice;
import by.grsu.hdm.model.INetworkAvailable;
import by.grsu.hdm.model.IUsbPort;
import by.grsu.hdm.model.IUsbSupport;
import by.grsu.hdm.model.Media;
import by.grsu.hdm.model.MediaPlayer;
import by.grsu.hdm.model.UsbPort;

public class MediaCenter extends HomeDevice implements IUsbSupport, INetworkAvailable {
	
	private long trafficIn;
	private long trafficOut;
	private MediaPlayer mediaPlayer;
	private UsbPort usbPort;
	
	protected MediaCenter() {
		this.mediaPlayer = new MediaPlayer();
		defaultConfiguration();
	}
	
	public MediaCenter(String id) {
		super(id);
		this.mediaPlayer = new MediaPlayer();
		defaultConfiguration();
	}
	
	protected void defaultConfiguration()
	{
		this.trafficIn = 0;
		this.trafficOut = 0;
		
		this.usbPort = null;
	}
	
	@Override
	public long getOutgoingTraffic() {
		return this.trafficOut;
	}
	
	@Override
	public long getIncomingTraffic() {
		return this.trafficIn;
	}

	@Override
	protected void afterTurningOn() {
		super.afterTurningOn();
		this.trafficIn = Math.abs(new Random().nextInt());
		this.trafficOut = Math.abs(new Random().nextInt());
	}

	@Override
	protected void beforeTurningOff() {
		super.beforeTurningOff();
		defaultConfiguration();
	}

	@Override
	public IUsbPort getUsbPort() {
		if (this.usbPort == null) { 
			this.usbPort = new UsbPort("USB-" + getDeviceId());
		}
		
		return this.usbPort;
	}
	public void addFormat(String format)
	{
		this.mediaPlayer.addFormat(format);
	}

	public void play(Media media) {
		if(mediaPlayer != null)
		this.mediaPlayer.play(media);
	}

	public void stop() {
		if(mediaPlayer != null)
		this.mediaPlayer.stop();
	}

	public void pause() {
		if(mediaPlayer != null)
		this.mediaPlayer.pause();
	}
	}
